<?php

namespace app\model\v1\repair;

use app\model\order\StoreOrder;
use app\model\user\User;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\facade\Db;
use think\Model;

class MaintenanceOrder extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    public function user()
    {
        return $this->hasOne (User::class,'uid','maintenance_user_id');
    }

    public function order()
    {
        return $this->hasOne (StoreOrder::class,'order_id','order_id');
    }


    /**
     * 维修单管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getMaintenanceOrderList($param = [])
    {
        $data["data"] = self::field ("*,
            case when is_active=1 then '启用' 
                when is_active=2 then '禁用' 
                end as is_active_text,
            case when status=1 then '待接单' 
                when status=2 then '接单' 
                when status=3 then '已上门' 
                when status=4 then '已维修' 
                when status=5 then '维修用户已确认' 
                when status=6 then '有问题' 
                end as status_text
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
                if(!empty($param["status"])) $query->where("status", $param["status"]);
            })
            ->with([
                'user'=> function($query) {
                    $query->field('uid,nickname,real_name,phone');
                },
                'order'=> function($query) {
                    $query->field('order_id,FROM_UNIXTIME(pay_time) as pay_time,pay_price');
                },
            ])
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
                if(!empty($param["status"])) $query->where("status", $param["status"]);
            })
            ->count();

        return $data;
    }


    /**
     * 维修单管理列表 - 编辑
     * @param $param
     * @return ConfigBanner
     */
    public static function updateMaintenanceOrder($param = [])
    {
        if(!empty($param["order_id"]))              $update["order_id"] = $param["order_id"];
        if(!empty($param["maintenance_user_id"]))   $update["maintenance_user_id"] = $param["maintenance_user_id"];
        if(!empty($param["status"]))                $update["status"] = $param["status"];
        if(!empty($param["img"]))                   $update["img"] = $param["img"];
        if(!empty($param["is_shangmen_chaoshi"]))   $update["is_shangmen_chaoshi"] = $param["is_shangmen_chaoshi"];
        if(!empty($param["shangmen_time"]))   $update["shangmen_time"] = $param["shangmen_time"];
        if(!empty($param["is_accept_status"]))      $update["is_accept_status"] = $param["is_accept_status"];

        return self::where("id", $param['id'])->update($update);
    }



    /**
     * 维修单管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addRepairService($param = [])
    {
        $add = [
            "relation_id" => $param["relation_id"] ?? 0,
            "avatar" => $param["avatar"] ?? '',
            "nickname" => $param["nickname"] ?? '',
            "phone" => $param["phone"] ?? '',
            "user_skill" => $param["user_skill"] ?? '',
            "status_work" => $param["status_work"] ?? '',
            "add_time"   => time(),
            "type" => $param["type"] ?? '',
            "name" => $param["name"] ?? '',
            "native" => $param["native"] ?? '',
            "region" => $param["region"] ?? '',
        ];
        $userid = User::where("phone", $param["phone"])->value("uid") ?? 0;

        if(empty($userid)) Throw new Exception("系统内未找到此手机号用户！");
        $add["uid"] = $userid;
        return self::insert($add);
    }




    /**
     * 维修单管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableMaintenanceOrder($param = [])
    {
        $status = self::where("id", $param['id'])->value("is_active");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['is_active' => $enable]);
        return $msg;
    }




}